﻿using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using Avalonia.Threading;
using ScottPlot;
using ScottPlot.Avalonia;
using ScottPlot.FontResolvers;
using ScottPlot.Plottables;
using SkiaSharp;
using System;
using System.Linq;
using System.Threading.Tasks;
using Colors = ScottPlot.Colors;

namespace genshin_gacha.Views;

public partial class MainView : UserControl
{
    private CharacterPrService characterPrService;
    public MainView()
    {
        InitializeComponent();

    }
    private Signal signal1 = null;
    private Signal signal2 = null;
    private HorizontalLine prline1 = null;
    private HorizontalLine prline2 = null;
    private VerticalLine qwline1 = null;
    private VerticalLine qwline2 = null;


    private Signal weapon_signal1 = null;
    private Signal weapon_signal2 = null;
    private HorizontalLine weapon_prline1 = null;
    private HorizontalLine weapon_prline2 = null;
    private VerticalLine weapon_qwline1 = null;
    private VerticalLine weapon_qwline2 = null;

    private double _winPr = 0.55;
    private bool _isTotalPr = false;
    private bool _isWeaponTotalPr = false;

    protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
    {
        base.OnApplyTemplate(e);

        slider.ValueChanged += OnSliderValueChange;
        reclacBtn.Click += ReclacBtn_Click;
        cbTotalPr.IsCheckedChanged += (s, e) =>  _isTotalPr = cbTotalPr.IsChecked.Value;

        reclacWeaponBtn.Click += ReclacWeaponBtn_Click;
        cbWTotalPr.IsCheckedChanged += (s, e) => _isWeaponTotalPr = cbTotalPr.IsChecked.Value;
        //Task.Run(() =>
        //{
        //    characterPrService = new CharacterPrService();
        //    var basePrArr = Enumerable.Range(1, 180).Select(i => characterPrService.GetUpPR(i)).ToArray();
        //    Dispatcher.UIThread.Invoke(() =>
        //    {
        //        
        //        AvaPlot.Refresh();
        //    });

        //});
        //var fontFamily = new FontFamily("avares://genshin_gacha/Assets/NotoSansMonoCJKsc-Regular.otf#Noto Sans Mono CJK SC");
        var font = this.FindResource("NotoSansMono");
        //var sk = SKTypeface.FromFamilyName(fontFamily.Name);


        AvaPlot.UserInputProcessor.IsEnabled = true;
       
        AvaPlot.Plot.XLabel("抽卡数");
        //AvaPlot.Plot.Title("UP角色的抽卡概率");
        AvaPlot.Plot.Axes.SetLimitsX(0, 180);
        AvaPlot.Plot.Axes.SetLimitsY(0, 0.1);


        //axLine2.LabelOppositeAxis = true;


        var legend = AvaPlot.Plot.ShowLegend();
        legend.Location = Alignment.UpperRight;

        this.Calc();
        

        AvaPlot.Plot.Font.Automatic();
        AvaPlot.Plot.Axes.Left.TickGenerator = new ScottPlot.TickGenerators.NumericAutomatic()
        {
            LabelFormatter = d => $"{d * 100:0.00}%"
        };
        AvaPlot.Refresh();


        AvaPlotWeapon.UserInputProcessor.IsEnabled = true;

        AvaPlotWeapon.Plot.XLabel("抽卡数");
        //AvaPlotWeapon.Plot.Title("UP武器的抽卡概率");
        AvaPlotWeapon.Plot.Axes.SetLimitsX(0, 240);
        AvaPlotWeapon.Plot.Axes.SetLimitsY(0, 0.1);


        //axLine2.LabelOppositeAxis = true;


        var legendWeapon = AvaPlotWeapon.Plot.ShowLegend();
        legendWeapon.Location = Alignment.UpperRight;

        this.WeaponCalc();


        AvaPlotWeapon.Plot.Font.Automatic();
        AvaPlotWeapon.Plot.Axes.Left.TickGenerator = new ScottPlot.TickGenerators.NumericAutomatic()
        {
            LabelFormatter = d => $"{d * 100:0.00}%"
        };
        AvaPlotWeapon.Refresh();
    }


    private void ReclacBtn_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        this.Calc();
        if (this._isTotalPr)
        {
            AvaPlot.Plot.Axes.SetLimitsY(0, 1);
            AvaPlot.Plot.Axes.AutoScale();
        }
        else
        {
            AvaPlot.Plot.Axes.SetLimitsY(0, 0.1);
            AvaPlot.Plot.Axes.AutoScale();
        }
        AvaPlot.Refresh();
    }

    private void Calc()
    {
        characterPrService = new CharacterPrService();
        characterPrService.ClearCache();
        var basePrArr = Enumerable.Range(1, 180).Select(i => characterPrService.GetUpPR(i)).ToArray();
        var qiwang1 = Enumerable.Range(1, 180).Select(i => basePrArr[i - 1] * i).Sum();
        var maxPr1 = basePrArr.Max();
        if (this._isTotalPr)
        {
            for(int i=1;i<basePrArr.Length; i++)
            {
                basePrArr[i] = basePrArr[i] + basePrArr[i - 1];
            }
        }
        if (signal1 != null)
        {
            AvaPlot.Plot.Remove(signal1);
        }
        signal1 = AvaPlot.Plot.Add.Signal(basePrArr);
        signal1.LineColor = Colors.Blue;
        signal1.Label = "4.8 Gacha";

        if (prline1 != null)
        {
            AvaPlot.Plot.Remove(prline1);
        }
        prline1 = AvaPlot.Plot.Add.HorizontalLine(maxPr1);
        prline1.LabelFontName = "Noto Sans Mono CJK SC";
        prline1.LabelAlignment = Alignment.MiddleRight;
        prline1.Text = $"{maxPr1 * 100:0.####}%";
        prline1.TextRotation = 0;
        prline1.Color = Colors.Blue;
        prline1.LinePattern = LinePattern.Dashed;
        //axLine1.LabelOppositeAxis = true;
        if (qwline1 != null)
        {
            AvaPlot.Plot.Remove(qwline1);
        }
        qwline1 = AvaPlot.Plot.Add.VerticalLine(qiwang1);
        qwline1.LabelFontName = "Noto Sans Mono CJK SC";
        qwline1.LabelAlignment = Alignment.MiddleRight;
        qwline1.Text = $"{qiwang1:0.##}";
        qwline1.TextRotation = 0;
        qwline1.Color = Colors.Blue;
        qwline1.LinePattern = LinePattern.Dotted;


        characterPrService = new CharacterPrService();
        characterPrService.WinPR = _winPr;
        var basePrArr2 = Enumerable.Range(1, 180).Select(i => characterPrService.GetUpPR(i)).ToArray();
        var qiwang2 = Enumerable.Range(1, 180).Select(i => basePrArr2[i - 1] * i).Sum();
        var maxPr2 = basePrArr2.Max();
        if (this._isTotalPr)
        {
            for (int i = 1; i < basePrArr2.Length; i++)
            {
                basePrArr2[i] = basePrArr2[i] + basePrArr2[i - 1];
            }
        }
        if (signal2 != null)
        {
            AvaPlot.Plot.Remove(signal2);
        }
        signal2 = AvaPlot.Plot.Add.Signal(basePrArr2);
        signal2.LineColor = Colors.Red;
        signal2.Label = "5.0 Gacha";

        if (prline2 != null)
        {
            AvaPlot.Plot.Remove(prline2);
        }
        prline2 = AvaPlot.Plot.Add.HorizontalLine(maxPr2);
        prline2.LabelFontName = "Noto Sans Mono CJK SC";
        prline2.LabelAlignment = Alignment.MiddleRight;
        prline2.Text = $"{maxPr2 * 100:0.####}%";
        prline2.TextRotation = 0;
        prline2.Color = Colors.Red;
        prline2.LinePattern = LinePattern.Dashed;

        if (qwline2 != null)
        {
            AvaPlot.Plot.Remove(qwline2);
        }
        qwline2 = AvaPlot.Plot.Add.VerticalLine(qiwang2);
        qwline2.LabelFontName = "Noto Sans Mono CJK SC";
        qwline2.LabelAlignment = Alignment.MiddleRight;
        qwline2.Text = $"{qiwang2:0.##}";
        qwline2.TextRotation = 0;
        qwline2.Color = Colors.Red;
        qwline2.LinePattern = LinePattern.Dotted;

        this.txtOldC0.Text = $"{qiwang1:0.##}抽";
        this.txtOldC6.Text = $"{qiwang1*7:0.##}抽({qiwang1 * 7/50.5:0.##}×648)";
        this.txtNewC0.Text = $"{qiwang2:0.##}抽";
        this.txtNewC6.Text = $"{qiwang2*7:0.##}抽({qiwang2 * 7 / 50.5:0.##}×648)";
    }

    private void ReclacWeaponBtn_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        this.WeaponCalc();
        if (this._isWeaponTotalPr)
        {
            AvaPlotWeapon.Plot.Axes.SetLimitsY(0, 1);
            AvaPlotWeapon.Plot.Axes.AutoScale();
        }
        else
        {
            AvaPlotWeapon.Plot.Axes.SetLimitsY(0, 0.1);
            AvaPlotWeapon.Plot.Axes.AutoScale();
        }
        AvaPlotWeapon.Refresh();
    }

    private void WeaponCalc()
    {
        var weaponPrService = new WeaponPrService();

        var basePrArr = Enumerable.Range(1, 240).Select(i => weaponPrService.GetUpPR(i)).ToArray();
        var qiwang1 = Enumerable.Range(1, 240).Select(i => basePrArr[i - 1] * i).Sum();
        var maxPr1 = basePrArr.Max();
        if (this._isWeaponTotalPr)
        {
            for (int i = 1; i < basePrArr.Length; i++)
            {
                basePrArr[i] = basePrArr[i] + basePrArr[i - 1];
            }
        }
        if (weapon_signal1 != null)
        {
            AvaPlotWeapon.Plot.Remove(weapon_signal1);
        }
        weapon_signal1 = AvaPlotWeapon.Plot.Add.Signal(basePrArr);
        weapon_signal1.LineColor = Colors.Blue;
        weapon_signal1.Label = "4.8 Gacha";

        if (weapon_prline1 != null)
        {
            AvaPlotWeapon.Plot.Remove(weapon_prline1);
        }
        weapon_prline1 = AvaPlotWeapon.Plot.Add.HorizontalLine(maxPr1);
        weapon_prline1.LabelFontName = "Noto Sans Mono CJK SC";
        weapon_prline1.LabelAlignment = Alignment.MiddleRight;
        weapon_prline1.Text = $"{maxPr1 * 100:0.####}%";
        weapon_prline1.TextRotation = 0;
        weapon_prline1.Color = Colors.Blue;
        weapon_prline1.LinePattern = LinePattern.Dashed;
        //axLine1.LabelOppositeAxis = true;
        if (weapon_qwline1 != null)
        {
            AvaPlotWeapon.Plot.Remove(weapon_qwline1);
        }
        weapon_qwline1 = AvaPlotWeapon.Plot.Add.VerticalLine(qiwang1);
        weapon_qwline1.LabelFontName = "Noto Sans Mono CJK SC";
        weapon_qwline1.LabelAlignment = Alignment.MiddleRight;
        weapon_qwline1.Text = $"{qiwang1:0.##}";
        weapon_qwline1.TextRotation = 0;
        weapon_qwline1.Color = Colors.Blue;
        weapon_qwline1.LinePattern = LinePattern.Dotted;



        weaponPrService.MaxFailCount = 1;
        var basePrArr2 = Enumerable.Range(1, 160).Select(i => weaponPrService.GetUpPR(i)).ToArray();
        var qiwang2 = Enumerable.Range(1, 160).Select(i => basePrArr2[i - 1] * i).Sum();
        var maxPr2 = basePrArr2.Max();
        if (this._isWeaponTotalPr)
        {
            for (int i = 1; i < basePrArr2.Length; i++)
            {
                basePrArr2[i] = basePrArr2[i] + basePrArr2[i - 1];
            }
        }
        if (weapon_signal2 != null)
        {
            AvaPlotWeapon.Plot.Remove(signal2);
        }
        weapon_signal2 = AvaPlotWeapon.Plot.Add.Signal(basePrArr2);
        weapon_signal2.LineColor = Colors.Red;
        weapon_signal2.Label = "5.0 Gacha";

        if (weapon_prline2 != null)
        {
            AvaPlotWeapon.Plot.Remove(weapon_prline2);
        }
        weapon_prline2 = AvaPlotWeapon.Plot.Add.HorizontalLine(maxPr2);
        weapon_prline2.LabelFontName = "Noto Sans Mono CJK SC";
        weapon_prline2.LabelAlignment = Alignment.MiddleRight;
        weapon_prline2.Text = $"{maxPr2 * 100:0.####}%";
        weapon_prline2.TextRotation = 0;
        weapon_prline2.Color = Colors.Red;
        weapon_prline2.LinePattern = LinePattern.Dashed;

        if (weapon_qwline2 != null)
        {
            AvaPlotWeapon.Plot.Remove(weapon_qwline2);
        }
        weapon_qwline2 = AvaPlotWeapon.Plot.Add.VerticalLine(qiwang2);
        weapon_qwline2.LabelFontName = "Noto Sans Mono CJK SC";
        weapon_qwline2.LabelAlignment = Alignment.MiddleRight;
        weapon_qwline2.Text = $"{qiwang2:0.##}";
        weapon_qwline2.TextRotation = 0;
        weapon_qwline2.Color = Colors.Red;
        weapon_qwline2.LinePattern = LinePattern.Dotted;

        this.txtOldW1.Text = $"{qiwang1:0.##}抽";
        this.txtOldW5.Text = $"{qiwang1 * 5:0.##}抽({qiwang1 * 5 / 50.5:0.##}×648)";
        this.txtNewW1.Text = $"{qiwang2:0.##}抽";
        this.txtNewW5.Text = $"{qiwang2 * 5:0.##}抽({qiwang2 * 5 / 50.5:0.##}×648)";
    }

    private void OnSliderValueChange(object? sender, RangeBaseValueChangedEventArgs e)
    {
        var newVal = (int)e.NewValue;
        this.rateText.Text = $"{newVal:0}%";

        this._winPr = newVal / 100d;
    }
}